package com.slliver.commonservice.util;

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.Objects;

import org.apache.commons.io.FileUtils;

public class VeidoFileUtils {


    private VeidoFileUtils() {
    }

    private static final String SUCCESS = "success";


    public static void main(String[] args) throws IOException {
        //String filePath = "F:\\download\\20250407\\application.properties";
        String rootPath = "F:\\download\\";
        String filePath = rootPath + "20250407";
        String[] extensions = {"mp4"};
        //filePath = filePath + "\\" + batchMoveDirName;
        //String moveMsg = batchMoveFile(filePath, extensions, "love姐", "love姐");
        //System.out.println(moveMsg);

        String batchChangeDirName = "黄色仓库";
        String changeAfterDirName = rootPath+batchChangeDirName;
        String changeFileNameMsg = batchChangeFileName(filePath, extensions, "迅雷下载 -", changeAfterDirName);
        System.out.println(changeFileNameMsg);
    }

    public static String batchMoveFile(String fileRootPath, String[] extensions, String indexOfStr, File descDirName) {
        String validStr = validFilePath(fileRootPath);
        if (!Objects.equals(SUCCESS, validStr)) {
            return validStr;
        }

        File dir = new File(fileRootPath);
        Collection<File> fileList = FileUtils.listFiles(dir, extensions, false);
        System.out.println("valid ok, 共有 " + fileList.size() + " 个文件");

        int targetNum = 0;
        String destDir = fileRootPath + File.separator + descDirName;
        System.out.println("destDir = " + destDir);
        for (File file : fileList) {
            String fileName = file.getName();
            if (fileName.toLowerCase().indexOf(indexOfStr) > 0) {
                String fullFilePath = fileRootPath + File.separator + fileName;
                System.out.println("完整文件名== >>> " + fullFilePath);
                targetNum++;
                // 移动文件到指定文件夹中，并给定是否创建文件夹
                try {
                    FileUtils.moveFileToDirectory(new File(fullFilePath), new File(destDir), true);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        System.out.println("===================================================================");
        System.out.println("需要移动的文件个数 ===>>> " + targetNum);
        return SUCCESS;
    }

    private static String batchChangeFileName(String fileRootPath, String[] extensions, String indexOfStr, String descDirName) throws IOException {
        String validStr = validFilePath(fileRootPath);
        if (!Objects.equals(SUCCESS, validStr)) {
            return validStr;
        }

        File dir = new File(fileRootPath);
        Collection<File> fileList = FileUtils.listFiles(dir, extensions, false);
        System.out.println("valid ok, 共有 " + fileList.size() + " 个文件");

        //fileList = fileList.stream().toList().subList(0,1);

        int targetNum = 0;
        String newFileName = "";
        for (File file : fileList) {
            String fileName = file.getName().toLowerCase();
            String fileExtension = FilenameUtils.getExtension(fileName);
            if (fileName.indexOf(indexOfStr) > 0) {
                System.out.println("修改前的文件名 === >>> " + fileName);
                System.out.println("修改前的文件后缀 === >>> " + fileExtension);
                String fullFilePath = fileRootPath + File.separator + fileName;
                System.out.println("原来的完整文件名== >>> " + fullFilePath);
                targetNum++;
                // 移动文件到指定文件夹中，并给定是否创建文件夹
                //FileUtils.moveFileToDirectory(new File(fullFilePath), new File(destDir), true);
                // 移动文件到指定文件夹中，并给定是否创建文件夹
                //FileUtils.moveFileToDirectory(new File("/Users/Downloads/海葡萄.jpeg"),new File(destDir),false);
                newFileName = descDirName + File.separator + fileName.substring(0, fileName.indexOf(indexOfStr)) + "." + fileExtension;
                System.out.println("修改后的文件名 === >>> " + newFileName);
                FileUtils.moveFile(file, new File(newFileName));
            }
        }
        System.out.println("===================================================================");
        System.out.println("需要修改文件名的文件个数 ===>>> " + targetNum);

        return SUCCESS;
    }

    private static String validFilePath(String fileRootPath) {
        if (StringUtils.isBlank(fileRootPath)) {
            return "Error: =>> 不合法的文件路径";
        }
        // 检查文件夹是否存在
        File dir = new File(fileRootPath);
        if (!dir.exists()) {
            return "Error: =>> 不合法的文件路径,当前文件路径不存在";
        }
        // 判断是否文件夹
        if (!dir.isDirectory()) {
            return "Error: =>> 不合法的文件路径, 传入的文件夹路径不合法";
        }
        // 路径下是否有文件
        long size = FileUtils.sizeOfDirectory(dir);
        if (size == 0) {
            return "Error: =>> 不合法的文件路径, 当前文件夹下没有文件";
        }
        return "success";
    }
}
